package org.xurui.sxl.protocol.command.impl;

import org.springframework.util.StringUtils;
import org.xurui.sxl.protocol.utils.BinaryUtils;
import org.xurui.sxl.protocol.utils.LonLat;
import org.xurui.sxl.protocol.utils.NumberUtils;

import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class testDemo {
    public static void main(String[] args) {
      /*      String n="11341.8141";
            String t="3442.7128";
    int lo=Integer.parseInt(n.substring(0,3));
    int la=Integer.parseInt(t.substring(0,2));

    double ln=Double.parseDouble(n.substring(3,n.length()));
    double lt=Double.parseDouble(t.substring(2,t.length()));
        System.out.println(lo);
        System.out.println(la);
        System.out.println(ln);
        System.out.println(lt);


        System.out.println(ln/60);
        System.out.println(String.format("%.4f",lo+ln/60));
        System.out.println("********************");
        System.out.println(lt/60);
        System.out.println(la+lt/60);*/
        /*SimpleDateFormat sdf=new SimpleDateFormat("dd");
        Calendar cal=Calendar.getInstance();
        int month=cal.get(Calendar.MONTH)+1;
        System.out.println(month);*/
        /*Date date = new Date();
        cal.setTime(date);
        int dayWeek=cal.get(Calendar.DAY_OF_WEEK);
        if(1==dayWeek){
            cal.add(Calendar.DAY_OF_MONTH,-1);
        }
        System.out.println("要计算日期为："+sdf.format(cal.getTime()));
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day=cal.get(Calendar.DAY_OF_WEEK);
        System.out.println("天："+day);
        System.out.println(cal.getFirstDayOfWeek());
        cal.add(Calendar.DATE,cal.getFirstDayOfWeek()-day);
        String imptimeBegin=sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期："+imptimeBegin);
        cal.add(Calendar.DATE,1);
        String imptimeEr=sdf.format(cal.getTime());
        System.out.println("所在周星期二的日期："+imptimeEr);
        cal.add(Calendar.DATE,1);
        String imptimeMi=sdf.format(cal.getTime());
        System.out.println("所在周星期三的日期："+imptimeMi);
        cal.add(Calendar.DATE,1);
        String imptimeS=sdf.format(cal.getTime());
        System.out.println("所在周星期四的日期："+imptimeS);
        cal.add(Calendar.DATE,1);
        String imptimeEnd=sdf.format(cal.getTime());
        System.out.println("所在周星期五的日期："+imptimeEnd);*/
          /*  String ads="TP,869727050490554,V4,CLASS,174255,1,0800083011213141516070,2,0850092011213141516070,174257,V,,,,,,99.99,310322,FDFFFD11";
        String[] payloadArr=ads.split(",");

        String[] paraArr= Arrays.copyOfRange(payloadArr,4,payloadArr.length-10);
        String para="";
        for (int i=1;i<paraArr.length;i++){
            // System.out.println(paraArr[i]);
            if(i==1){
                para=paraArr[i];
            }else{
                para=para+","+paraArr[i];
            }
        }
        int j=4+paraArr.length;

        Map<String, Object> properties = new HashMap<>();
        properties.put("para",para);
        properties.put("uptime",payloadArr[j]);
        properties.put("s",payloadArr[j+1]);
        properties.put("latitude",payloadArr[j+2]);
        properties.put("d",payloadArr[j+3]);
        properties.put("longitude",payloadArr[j+4]);
        properties.put("g",payloadArr[j+5]);
        properties.put("speed", NumberUtils.parseDouble(payloadArr[j+6]));
        properties.put("pdop",NumberUtils.parseDouble(payloadArr[j+7]));
        properties.put("upday", payloadArr[j+8]);
        String trackerStatusHex = payloadArr[j+9];*/
/*String payload="*TP,869727050490554,TEMPERTUREM,124549,8,34.41,37.14,37.17,37.20,37.23,37.27,37.30,37.34,110422,DFFDFF13";

        String[] payloadArr = payload.replaceAll("\\*", "").split(",");
        String deviceId = payloadArr[1];
        int temperturemNumber = NumberUtils.parseInt(payloadArr[4]);//数量
        List<Map> temperturemInfo = new ArrayList<>();
        for (int i = 0; i < temperturemNumber; i++) {
            Map k = new HashMap();
            k.put("temperatureNum", payloadArr[5 + i * 1]);
            temperturemInfo.add(k);
        }

        Map<String, Object> properties = new HashMap<>();
        properties.put("temperturemInfo", temperturemInfo);
        properties.put("upTime",payloadArr[3]);
        properties.put("upday",payloadArr[5+temperturemNumber]);
        String trackerStatusHex = payloadArr[6+temperturemNumber];
        System.out.println(trackerStatusHex);*/

        /*Map<String,Object> trackerStatusMap = parseTrackerStatus(trackerStatusHex);
        properties.put("trackerstatus",trackerStatusMap);*/

        /*String[] starTimes = new String[]{};
        String[] endTimes = new String[]{};
        System.out.println("赋值前："+starTimes.length);

        starTimes="2200,0800".split(",");
        endTimes="2300,1800".split(",");
        System.out.println("赋值后："+starTimes.length);

        if(starTimes.length==endTimes.length){
            System.out.println("长度相同");
        }

        int weekNum=7;

        String[] swFlagss="0,1,0,1,0,1,0;1,0,1,0,1,0,1".split(";");

        StringBuffer sb = new StringBuffer();
        sb.append("*");
        String param="";
        for (int j=0;j<starTimes.length;j++){
            sb.append(",");
            sb.append((j+1));
            String[] swFlags=swFlagss[j].split(",");
            StringBuffer bs = new StringBuffer();
            for (int i=1;i<weekNum;i++){
                bs.append(i+swFlags[i]);
            }
            sb.append(",");
            sb.append(starTimes[j]+endTimes[j]+bs.toString());
            *//*if(j==0){
                param=starTimes[j]+endTimes[j]+bs.toString();
            }else{
                param=param+","+starTimes[j]+endTimes[j]+bs.toString();
            }*//*

        }
        System.out.println(sb.toString());*/
        //System.out.println(param);

        /*String[] mesArr= "01,02,04,07,08,13".split(",");
        StringBuffer sb= new StringBuffer();
        boolean bs=false;
        for (int i=0;i<mesArr.length;i++){
            sb.append(",");
            sb.append(i+1);
            bs=BinaryUtils.isMNC("1",mesArr[i]);
            if(!bs){
                break;
            }
            sb.append(",");
            sb.append(mesArr[i]);

        }
        if(!bs){
            log.error("该参数MNC存在错误请根据功能开关进行传递！");
        }
        System.out.println(sb.toString());*/
        /*SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");
        //Date date=new Date();
        String time=sdf.format(new Date());
        //String time = DateFormatUtils.format(new Date(),"DDMMYY");
        System.out.println(time);*/
        /*String aa="08:00";
        System.out.println(deleteString2(aa,':'));*/
        /*String day="Mon_Tue_Wed_Thu_Fri";
        System.out.println(swFlag(day));;;*/
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String day="2022-03-01";
        ParsePosition pos=new ParsePosition(0);
        Date dd=sdf.parse(day,pos);
        System.out.println(tomorrowDateStr(dd));*/

        //System.out.println( dateSwitch("160819-130306"));;
       /* SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String day="2022-03-27";
        ParsePosition pos=new ParsePosition(0);
        Date dd=sdf.parse(day,pos);
        System.out.println(getWeek(dd));*/
// 获取本周日期数组
        /*List<String> weeks = getFormatDate("week");
        System.out.println(JSONObject.toJSONString(weeks));*/
        //System.out.println(getThisWeekDate());
       /* Map<String ,Object> map=null;//new HashMap<>();
        *//*map.put("modeN","m1");
        map.put("interval",300);*//*
        System.out.println(map);
        FunctionInvokeMessage invokeMessage=new FunctionInvokeMessage();
        List<FunctionParameter> inputs = new ArrayList();
        if(!StringUtils.isNullOrEmpty(map)){
            Set keySet=map.keySet();
            Iterator iterator=keySet.iterator();
            while (iterator.hasNext()){
                String key=iterator.next().toString();
                Object value=map.get(key);
                System.out.println("key:"+key+"---->"+value);
                FunctionParameter param = new FunctionParameter();
                param.setName(key);
                param.setValue(value);
                inputs.add(param);
            }



            invokeMessage.setInputs(inputs);
        }

        System.out.println(invokeMessage);*/
        /*
        String n="116.433322";
            String t="39.900255";*/


      /*  LonLat lonLat=new LonLat();
        lonLat.lng=116.433322;
            lonLat.lat=39.900255;

            LonLat aa=BinaryUtils.gcj02towgs84(lonLat);
        System.out.println(aa.lat);
        System.out.println(aa.lng);

        System.out.println("*************************");
        String lon=BinaryUtils.getLonLatToWgs84(String.valueOf(aa.lng),"lon");
        System.out.println(lon);
        String ln=BinaryUtils.getWgs84LonLat(lon,"lon");
        System.out.println(ln);
        String lat=BinaryUtils.getLonLatToWgs84(String.valueOf(aa.lat),"lat");
        System.out.println(lat);
        String lt=BinaryUtils.getWgs84LonLat(lat,"lat");
        System.out.println(lt);*/

        /*String as="113.68288,34.738876,113.682816,34.737871,113.684575,34.737906,113.684511,34.738946";
        String [] conList=as.split(",");
        System.out.println(conList);
        for (int i=0;i<conList.length;i++){
            LonLat lonLat=new LonLat();
            lonLat.lat=Double.parseDouble(conList[i+1]);
            lonLat.lng=Double.parseDouble(conList[i]);
            LonLat aa=BinaryUtils.gcj02towgs84(lonLat);

            String lat=BinaryUtils.getLonLatToWgs84(String.valueOf(aa.lat),"lat");
            conList[i]=lat;
            System.out.println(lat);
            String lon=BinaryUtils.getLonLatToWgs84(String.valueOf(aa.lng),"lon");
            conList[i+1]=lon;
            System.out.println(lon);
            i++;

        }

        System.out.println("******************************8");
        System.out.println(conList);*/


       /* String wlPhone="15093283491,13588888888,13583333333,17839961613,13583728327,_,_,_,_,_,_,_";
        String wlType="1,1,1,1,2,_,_,_,_,_,_,_";
        String[] wlPhones=wlPhone.toString().split(",");
        System.out.println(wlPhones.length);

        for (int i=0;i<wlPhones.length;i++){
            String aa="";
            if(!"_".equals(wlPhones[i])){
                aa=wlPhones[i];
                System.out.println(aa);
            }else{
                System.out.println("33333333333");
                System.out.println(aa);
            }
        }*/
        /*String[] mac = new String[]{};
        System.out.println(mac.length);*/
        //*HQ,866104040476544,WLALL,181120,1,17839961613,1,2,15093283491,1
        /*SimpleDateFormat dateFormat=new SimpleDateFormat("HHmmss");
        String time = dateFormat.format(new Date());
        System.out.println(time);*/
       /* String[] timeWeekSwFlags=",,,,,,,,,,".split(",");
        System.out.println(timeWeekSwFlags.length);*/
        //System.out.println(BinaryUtils.getDateFormat());
    String payload="*TP,869727050742186,V3,100922,08,100643,310822,3027.4786,11422.1163,,,,,3027.4810,11422.1159,3027.4830,11422.1144,3027.4825,11422.1096,3027.4813,11422.1120,,,310822,DFFDDD9B";

        String[] payloadArr = payload.replaceAll("\\*", "").split(",");

        int postnum= 0;
        System.out.println(postnum);

        String[] paraArr= Arrays.copyOfRange(payloadArr,6,payloadArr.length-2);
        String para="";
        int num=1;
        for (int i=1;i<paraArr.length;i++){
            // System.out.println(paraArr[i]);
            if(i%2==0){
                if(!StringUtils.isEmpty(BinaryUtils.getWgs84LonLat(paraArr[i],"lon"))) {
                    System.out.println("longitude" + num);
                    System.out.println(BinaryUtils.getWgs84LonLat(paraArr[i], "lon"));
                    num = num + 1;
                }
            }else{
                if(!StringUtils.isEmpty(BinaryUtils.getWgs84LonLat(paraArr[i],"lat"))) {
                    System.out.println("latitude" + num);
                    System.out.println(BinaryUtils.getWgs84LonLat(paraArr[i], "lat"));
                }
            }

        }
        System.out.println(num);
        int j=6+paraArr.length;
        System.out.println("upday");
        System.out.println(payloadArr[j]);
        for(int i=1;i<num;i++){
            System.out.println(i);
        }
    }



    public static List<Map<String,Object>> getThisWeekDate(){
        List<Map<String,Object>> weekList=new ArrayList<>();
        Map map=new HashMap();
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //获取本周的周一日期
        Date date = new Date();//getThisWeekMonday();
        cal.setTime(date);
        int day = 0;
        try {
            for (int i = 0; i <7; i++) {
                day = cal.get(Calendar.DATE);
                if(i==0){
                    cal.set(Calendar.DATE, day + i);
                }else{
                    cal.set(Calendar.DATE, day + 1);
                }
                String dayAfter = dateFormat.format(cal.getTime());
                Date dateTime = dateFormat.parse(dayAfter);
                map.put(getWeek(dateTime),dateFormat.format(dateTime));
                //System.out.println("本周日期: "+dateFormat.format(dateTime)+" "+getWeek(dateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        weekList.add(map);
        return weekList;
    }

    /**
     * 获取本周周一的日期(方法二)
     */
    public static Date getThisWeekMonday() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        System.out.println("今天日期: "+dateFormat.format(cal.getTime())+" "+getWeek(cal.getTime()));
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        System.out.println("本周一: "+dateFormat.format(cal.getTime())+" "+getWeek(cal.getTime()));
        return cal.getTime();
    }


    /**
     * 根据日期类型获取日期范围
     *
     * @param type day:当天  week:本周（默认）  month:本月
     * @return
     */
    public static List<String> getFormatDate(String type) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<String> list = new ArrayList<>();

        if ("day".equals(type)) {
            list.add(sdf.format(calendar.getTime()));

        } else if ("month".equals(type)) {
            int index = calendar.get(Calendar.DAY_OF_MONTH);
            for (int i = 0; i < index; i++) {
                list.add(sdf.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }

        } else {
            int index = calendar.get(Calendar.DAY_OF_WEEK) == 1 ? 7 : calendar.get(Calendar.DAY_OF_WEEK) - 1;
            for (int i = 0; i < index; i++) {
                list.add(sdf.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, -1);
            }
        }

        // 日期翻转，本来是从大到小，翻转成从小到大
        Collections.reverse(list);
        return list;
    }

    public static String getWeek(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("EEEE");
        String week=sdf.format(date);
        return week;
    }


    public static String dateSwitch(String date){
        String[] dates=date.split("-");
        String ym=dates[0];
        String sf=dates[1];
        SimpleDateFormat inSdf=new SimpleDateFormat("ddmmyy", Locale.ENGLISH);
        SimpleDateFormat outSdf=new SimpleDateFormat("yyyy-mm-dd");
        String HH=sf.substring(0,2);
        String mm=sf.substring(2,4);
        String ss=sf.substring(4,6);
        Date paramDate=null;
        try {
            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.YEAR,-100);
            inSdf.set2DigitYearStart(calendar.getTime());


            paramDate=inSdf.parse(ym);
        }catch (ParseException e){
            System.out.println(e);
        }
        return  outSdf.format(paramDate)+" "+HH+":"+mm+":"+ss;
    }
    /**
     * 获取明天的日期字符串
     * @return
     */
    public static String tomorrowDateStr(Date date){
        //Date date=new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //把日期往后增加一天.整数往后推,负数往前移动(1:表示明天、-1：表示昨天，0：表示今天)
        calendar.add(Calendar.DATE,-1);

        //这个时间就是日期往后推一天的结果
        date=calendar.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String tomorrowStr = formatter.format(date);
        return tomorrowStr;
    }

    public static String swFlag(String str) {
        String[] we = "Mon_Tue_Wed_Thu_Fri_Sat_Sun".split("_");

        String[] days=str.split("_");
        StringBuffer stringBuffer = new StringBuffer("");
        String swFlags="";
            for (int i = 0; i < we.length; i++) {
                if(Arrays.asList(days).contains(we[i]) && "Mon".equals(we[i])){//星期一
                   swFlags="1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Mon".equals(we[i])) {
                    swFlags="0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Tue".equals(we[i])){//星期二
                    swFlags=swFlags+","+"1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Tue".equals(we[i])){
                    swFlags=swFlags+","+"0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Wed".equals(we[i])){
                    swFlags=swFlags+","+"1";
                }else  if(!Arrays.asList(days).contains(we[i]) && "Wed".equals(we[i])){//星期三
                    swFlags=swFlags+","+"0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Thu".equals(we[i])){
                    swFlags=swFlags+","+"1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Thu".equals(we[i])){//星期四
                    swFlags=swFlags+","+"0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Fri".equals(we[i])){
                    swFlags=swFlags+","+"1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Fri".equals(we[i])){//'星期五'
                    swFlags=swFlags+","+"0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Sat".equals(we[i])){
                    swFlags=swFlags+","+"1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Sat".equals(we[i])){//'星期六'
                    swFlags=swFlags+","+"0";
                }
                if(Arrays.asList(days).contains(we[i]) && "Sun".equals(we[i])){
                    swFlags=swFlags+","+"1";
                }else if(!Arrays.asList(days).contains(we[i]) && "Sun".equals(we[i])){//'星期天'
                    swFlags=swFlags+","+"0";
                }
            }

        return swFlags;
    }
}
